package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * employee表现层
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录系统方法
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //将用户前端输入密码进行md5加密
        String password = employee.getPassword();
        String passwordEncryption = DigestUtils.md5DigestAsHex(password.getBytes());

        //根据前端传过来的员工姓名为条件,执行sql语句
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        //执行sql语句后没有查询到数据
        if(one == null){
            return R.error("用户名不存在");
        }

        //查询到匹配数据,密码与匹配数据不符
        if(!passwordEncryption.equals(one.getPassword())){
            return R.error("用户名或密码错误");
        }

        //判断用户是否被禁用
        if(one.getStatus() == 0){
            return R.error("员工已禁用");
        }

        //将用户名传进session保存
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);

    }

    /**
     * 员工退出系统方法
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //移除服务器中名为"employee"的session
        request.getSession().removeAttribute("employee");
        return R.success("员工退出成功");

    }

    /**
     * 员工页面分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> listEmployee(Integer page,Integer pageSize,String name){
        //创建分页对象
        Page<Employee> pageBean = new Page<>(page, pageSize);
        //条件构造器,拼接员工姓名like %?%
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        //分页查询
        employeeService.page(pageBean,queryWrapper);
        //返回分页查询结果集对象
        return R.success(pageBean);
    }

    /**
     * 新增员工方法
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        //添加默认密码,并进行md5加密
        String password = "123456";
        String passwordEn = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(passwordEn);

        //设置更新时间和创建时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //从session中获取登录者id,设置更新人和创建人id
//        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);

        //执行添加sql语句
        boolean save = employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 修改员工数据方法
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updateEmployee (HttpServletRequest request,@RequestBody Employee employee){

//        employee.setUpdateTime(LocalDateTime.now());

        Long id = (Long) request.getSession().getAttribute("employee");
        if((id == employee.getId())&&(employee.getStatus() == 0)){
            return R.error("不能禁用自己");
        }
//        employee.setUpdateUser(id);

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 获取员工数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> listById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}
